#!/bin/bash

function add_blank()
{
	str=$1
	num=$2
	len=${#str}
	while [ $len -lt $num ]
	do
		str="$str "
		len=`expr $len + 1`
	done
}

function display()
{
	add_blank "$1" 15
	disuser="$str"
	add_blank "$2" 15
	disprog="$str"
	add_blank "$3"  15
	disallres="$str"
	echo "${disuser}${disprog}${disallres}"
}

ps aux | grep -v PID | awk '{print $1, $2, $5, $6, $11}'|sort > .tmp
lastuser=""
sumres=0
prgs=0
maxvsz=0

display "User" "progress_num" "all_res"
while read user pid vsz res cmd
do
	if [ "y$lastuser" != "y$user" ]
	then
		if [ "y$lastuser" != "y" ]
		then
			display "$lastuser" "$prgs" "$sumres"
		fi
		prgs=0
		sumres=0
		lastuser=$user
	fi
	prgs=`expr $prgs + 1`
	sumres=`expr $sumres + $res`
	
	if [ $maxvsz -lt $vsz ]
	then
		maxvsz=$vsz
		maxcmd=$cmd
		maxpid=$pid
	fi
done < .tmp
rm -f .tmp

display "$lastuser" "$prgs" "$sumres"
echo ""
echo "Max vsz process $maxpid  $maxcmd"